package com.itheima.service.impl.oms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.oms.OmsOrderReturnReasonMapper;
import com.itheima.pojo.oms.OmsOrderReturnReason;
import com.itheima.pojo.oms.vo.IdStatusVo;
import com.itheima.pojo.oms.vo.returnReason;
import com.itheima.service.oms.OmsOrderReturnReasonSrevice;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class OmsOrderReturnReasonSreviceImpl extends ServiceImpl<OmsOrderReturnReasonMapper, OmsOrderReturnReason> implements OmsOrderReturnReasonSrevice {
@Autowired
private OmsOrderReturnReasonMapper omsOrderReturnReasonMapper;


    /*添加退货原因*/
    @Override
    public void create(OmsOrderReturnReason returnReason) {
        OmsOrderReturnReason reason = new OmsOrderReturnReason();
        reason.setStatus(returnReason.getStatus());
        reason.setName(returnReason.getName());
        reason.setSort(returnReason.getSort());
        reason.setCreateTime(LocalDateTime.now());
        reason.setId(returnReason.getId());
        omsOrderReturnReasonMapper.insert(reason);
    }

    /*分页查询退货原因*/
    @Override
    public PageInfo listAll(int  pageNum,int  pageSize) {

        Page<OmsOrderReturnReason> objectPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OmsOrderReturnReason> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(OmsOrderReturnReason::getId);
        Page<OmsOrderReturnReason> page = this.page(objectPage, queryWrapper);
        List<OmsOrderReturnReason> records = page.getRecords();
        int total = (int)page.getTotal();
        int current = (int)page.getCurrent();
        PageInfo pageInfo1 = new PageInfo();
        pageInfo1.setList(records);
        pageInfo1.setTotal(total);
        pageInfo1.setPageNum(pageNum);
        pageInfo1.setPageSize(pageSize);
        pageInfo1.setTotalPage(current);
        return pageInfo1;
    }
    /*修改退货原因启用状态*/
    @Override
    public void updatestatus(Integer ids,Integer status) {

        LambdaQueryWrapper<OmsOrderReturnReason> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsOrderReturnReason::getId,ids);
        List<OmsOrderReturnReason> list = this.list(queryWrapper);
        List<OmsOrderReturnReason> list1 = list.stream().map(item -> {
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(list1);
    }
    /*修改退货原因*/
    @Override
    public void UpdateId(Integer id, returnReason returnReason) {
        LambdaQueryWrapper<OmsOrderReturnReason> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsOrderReturnReason::getId,id);
        List<OmsOrderReturnReason> list = this.list(queryWrapper);
        List<OmsOrderReturnReason> list1 = list.stream().map(item -> {
            item.setCreateTime(LocalDateTime.now());
            item.setSort(returnReason.getSort());
            item.setName(returnReason.getName());
            item.setStatus(returnReason.getStatus());
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(list1);
    }
}
